package com.keeson.android_cli.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lizi on 2016/9/1.
 */
public class UIHelper {
    public static void ToastMessage(Context cont, String msg) {


        // CustomTimeToast(cont,msg,3000);
        LogUtils.e("ToastMessage  msg:" + msg);
        showToast(cont, msg, 3000);

        /*Toast toast=Toast.makeText(cont, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 50);
        toast.show();*/


    }

    public static void ToastMessage(Context cont, int msg) {
        Toast.makeText(cont, msg, Toast.LENGTH_LONG).show();
    }

    public static void ToastMessage(Context cont, String msg, int time) {
        Toast.makeText(cont, msg, time).show();
    }

    public static void toActivityCommon(Context ctx, Class<?> clazz) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(ctx, clazz);
        ctx.startActivity(intent);
    }

    public static void toActivityLogin(Context ctx, Class<?> clazz) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setClass(ctx, clazz);
        ctx.startActivity(intent);
    }




    public static void toActivityCommonForResult(Context ctx, Class<?> clazz,
                                                 int requestCode) {
        Intent intent = new Intent();
        intent.setClass(ctx, clazz);
        ((AppCompatActivity) ctx).startActivityForResult(intent, requestCode);
    }

    public static void toActivitySerialCommon(Context context, Class<?> clazz,
                                              Object obj) {
        Intent intent = new Intent();
        intent.setClass(context, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        bundle.putSerializable("obj", (Serializable) obj);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void toActivityCommon(Context context, Class<?> clazz,
                                        HashMap<String, Object> obj) {
        Intent intent = new Intent();
        intent.setClass(context, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("obj", obj);
        context.startActivity(intent);
    }

    public static void toActivityCommon(Context context, Class<?> clazz, String obj) {
        Intent intent = new Intent();
        intent.setClass(context, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        bundle.putString("obj", obj);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void toActivityCommon2(Context context, Class<?> clazz, String obj) {
        Intent intent = new Intent();
        intent.setClass(context, clazz);
        Bundle bundle = new Bundle();
        bundle.putString("obj", obj);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void toActivityCall(Context context, String mobile) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                + mobile));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };

    public static void showToast(Context mContext, String text, int duration) {

        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(text);
        else
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
        mHandler.postDelayed(r, duration);

        mToast.show();
    }

    /*public static void showToast(Context mContext, int resId, int duration)
    {
        showToast(mContext, mContext.getResources().getString(resId), duration);
    }*/


    public static void CustomTimeToast(Context context, String string, int duration) {
        final Toast toast = Toast.makeText(context, string, Toast.LENGTH_LONG);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 10);// 3000表示点击按钮之后，Toast延迟3000ms后显示
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, duration);// 5000表示Toast显示时间为5秒

    }

}
